/* Turska UI library
 * Copyright 2003-2008 Jetro Lauha
 * All rights reserved.
 * Web: http://iki.fi/jetro/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of EITHER:
 *   (1) The GNU Lesser General Public License as published by the Free
 *       Software Foundation; either version 2.1 of the License, or (at
 *       your option) any later version. The text of the GNU Lesser
 *       General Public License is included with this library in the
 *       file LICENSE-LGPL.txt.
 *   (2) The BSD-style license that is included with this library in
 *       the file LICENSE-BSD.txt.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
 * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
 *
 * $Id: UI.cpp,v 1.2 2008/03/27 11:44:26 tonic Exp $
 * $Revision: 1.2 $
 */

#include <assert.h>
#include "SDL.h"
#include "SDL_opengl.h"
#include "UI.h"


UI::UI(UIGraphics *graphics) : mGraphics(graphics)
{
}


UI::~UI()
{
}


BOOL UI::handleUIPointerDown(INT x, INT y)
{
    BOOL componentHit = FALSE;
    UIComponentSet::iterator it = mActiveUIComponents.begin();
    UIComponentSet::iterator itend = mActiveUIComponents.end();
    for (; it != itend; ++it)
    {
        if ((*it)->isInside(x, y))
        {
            (*it)->onPointerDown(x, y);
            componentHit = TRUE;
        }
    }
    return componentHit;
}


BOOL UI::handleUIPointerUp(INT x, INT y)
{
    BOOL componentEvent = FALSE;
    UIComponentSet::iterator it = mActiveUIComponents.begin();
    UIComponentSet::iterator itend = mActiveUIComponents.end();
    for (; it != itend; ++it)
    {
        if ((*it)->isPointerDown())
            componentEvent = TRUE;
        (*it)->onPointerUp(x, y);
    }
    return componentEvent;
}


BOOL UI::handleUIPointerMove(INT x, INT y)
{
    BOOL componentEvent = FALSE;
    UIComponentSet::iterator it = mActiveUIComponents.begin();
    UIComponentSet::iterator itend = mActiveUIComponents.end();
    for (; it != itend; ++it)
    {
        if ((*it)->isPointerDown())
            componentEvent = TRUE;
        (*it)->onPointerMove(x, y);
    }
    return componentEvent;
}


void UI::addUIComponent(UIComponent *component)
{
    assert(component != NULL);
    if (component == NULL)
        return;
    mAllUIComponents.insert(component);
}


void UI::removeUIComponent(UIComponent *component)
{
    assert(component != NULL);
    if (component == NULL)
        return;
    UIComponentSet::iterator it = mAllUIComponents.find(component);
    assert(it != mAllUIComponents.end());
    if (it != mAllUIComponents.end())
        mAllUIComponents.erase(it);
}


void UI::activateUIComponent(UIComponent *component)
{
    // add to list of all ui components just to make sure this component is listed there too
    // (set datatype ignores duplicates which is a property needed for this operation)
    mAllUIComponents.insert(component);

    mActiveUIComponents.insert(component);
}


void UI::deactivateUIComponent(UIComponent *component)
{
    UIComponentSet::iterator it = mActiveUIComponents.find(component);
    if (it != mActiveUIComponents.end())
        mActiveUIComponents.erase(it);
}


void UI::activateAllComponents()
{
    UIComponentSet::iterator it = mAllUIComponents.begin();
    UIComponentSet::iterator itend = mAllUIComponents.end();
    for (; it != itend; ++it)
        activateUIComponent(*it);
}


void UI::deactivateAllComponents()
{
    mActiveUIComponents.clear();
}


void UI::deleteAllUIComponents()
{
    UIComponentSet::iterator it = mAllUIComponents.begin();
    UIComponentSet::iterator itend = mAllUIComponents.end();
    for (; it != itend; ++it)
        delete *it;
    mAllUIComponents.clear();
}


void UI::render()
{
    if (mGraphics != NULL)
        mGraphics->beginRender();

    UIComponentSet::iterator it = mActiveUIComponents.begin();
    UIComponentSet::iterator itend = mActiveUIComponents.end();
    for (; it != itend; ++it)
        (*it)->draw();

    if (mGraphics != NULL)
        mGraphics->endRender();
}
